package ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseFragment;
import com.fy.fyzf.bean.AreaListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.DemandListBean;
import com.fy.fyzf.bean.DemanddetailBean;
import com.fy.fyzf.bean.GetDemandDetailBean;
import com.fy.fyzf.bean.IsInfoBean;
import com.fy.fyzf.bean.MyRecommendBean;
import com.fy.fyzf.bean.ReleaseBean;
import com.fy.fyzf.dialog.DeleteWarningDialog;
import com.fy.fyzf.presenter.DemandPresenter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.view.DemandCallBack;
import com.fy.fyzf.view.SpacesItemDecoration;
import com.fy.fyzf.weight.SlideRecyclerView;

import java.util.List;

import butterknife.BindView;
import ui.activity.HouseDetailActivity;
import ui.adapter.AllDemandAdapter2;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DemandDetailFragment2#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DemandDetailFragment2 extends BaseFragment<DemandPresenter> implements DemandCallBack {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "isMy";
    private static final String ARG_PARAM2 = "releaseId";
    @BindView(R.id.recycleView)
    RecyclerView recycleView;

    // TODO: Rename and change types of parameters
    private Boolean mParam1;
    private Integer mParam2;
    private AllDemandAdapter2 allDemandAdapter2;

    public DemandDetailFragment2() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment DemandDetailDragment.
     */
    // TODO: Rename and change types and number of parameters
    public static DemandDetailFragment2 newInstance(boolean isMy, Integer id) {
        DemandDetailFragment2 fragment = new DemandDetailFragment2();
        Bundle args = new Bundle();
        args.putBoolean(ARG_PARAM1, isMy);
        args.putInt(ARG_PARAM2, id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getBoolean(ARG_PARAM1);
            mParam2 = getArguments().getInt(ARG_PARAM2);
        }
    }


    @Override
    protected DemandPresenter createPresenter() {
        return new DemandPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_demand_detail_dragment2;
    }

    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        allDemandAdapter2 = new AllDemandAdapter2(getActivity());
        recycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycleView.addItemDecoration(new SpacesItemDecoration(20));
        recycleView.setAdapter(allDemandAdapter2);

        allDemandAdapter2.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                List<MyRecommendBean.ListBean> data = adapter.getData();
                    showDeleteDialog(position,data.get(position).getResponseId());
            }
        });
/*
        allDemandAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<MyRecommendBean.ListBean> data = adapter.getData();
                Intent intent = new Intent(getActivity(), HouseDetailActivity.class);
                intent.putExtra("housingid", data.get(position).getHousingId());
                intent.putExtra("type", 1);
                ImageView imageView = view.findViewById(R.id.iv_logo);
                startActivity(intent);
            }
        })*/;
    }
    private ReleaseBean releaseBean = new ReleaseBean();
    private void showDeleteDialog(int position,int id){
        DeleteWarningDialog deleteWarningDialog = new DeleteWarningDialog(getActivity());
        deleteWarningDialog.setTitle("确定关闭该需求？");
        deleteWarningDialog.setContent("关闭该需求后\n" +
                "对方将无法看到您推荐的房源 !\n");
        deleteWarningDialog.toggleDialog();
        deleteWarningDialog.getView(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteWarningDialog.toggleDialog();
                releaseBean.setReleaseId(id);
                mPresenter.deleteDemand(releaseBean,position);
            }
        });
    }
    private GetDemandDetailBean commonSearchBean = new GetDemandDetailBean();
    @Override
    protected void loadData() {
        commonSearchBean.setIsMy(mParam1);
        commonSearchBean.setReleaseId(mParam2);
        commonSearchBean.setPageSize(100);
        mPresenter.listMyRecommend(commonSearchBean);
    }

    @Override
    public void getDemandListSuccess(DemandListBean demandListBean) {

    }

    @Override
    public void issueDemandSuccess(BaseData data) {

    }

    @Override
    public void getAreaListSuccess(List<AreaListBean> listBeans) {

    }

    @Override
    public void getDamendDetailSuccess(DemanddetailBean demanddetailBean) {

    }



    @Override
    public void getMyDemandListSuccess(DemandListBean demandListBean) {

    }

    @Override
    public void deleteDemandSuccess(BaseData data, int i) {
        allDemandAdapter2.remove(i);
    }

    @Override
    public void getMyRecommendSuccess(MyRecommendBean myRecommendBean) {

        if (myRecommendBean.getList() == null || myRecommendBean.getList().size() == 0) {
            allDemandAdapter2.setEmptyView(R.layout.layout_nodata_common,recycleView);
        }
        allDemandAdapter2.setNewData(myRecommendBean.getList());

    }

    @Override
    public void isInfoSuccess(IsInfoBean isInfoBean) {

    }
}